package com.yikome.shop.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Preconditions;
import com.itmuch.lightsecurity.annotation.PreAuthorize;
import com.yikome.shop.dto.CollectionCoverDTO;
import com.yikome.shop.dto.CollectionDTO;
import com.yikome.shop.dto.CollectionEditDTO;
import com.yikome.shop.entity.Collection;
import com.yikome.shop.entity.Present;
import com.yikome.shop.request.CollectionRequest;
import com.yikome.shop.service.CollectionService;
import com.yikome.shop.service.PresentService;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * Collection
 */
@Slf4j
@RestController
@RequestMapping("/colletion")
public class CollectionController {

	@Autowired 
	private CollectionService collectionService; 
	
	@Autowired 
	private PresentService presentService; 
	
	@GetMapping
	@PreAuthorize("hasAnyRoles('admin','analysis','shop', 'affiliate')")
	public Page<CollectionDTO> findCollection(CollectionRequest collectionRequest , @PageableDefault(sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable){
		return collectionService.findCollection(collectionRequest , pageable);
	}
	
	@GetMapping("/{collectionId}")
	@PreAuthorize("hasAnyRoles('admin','analysis','shop', 'affiliate')")
	public CollectionEditDTO findCollection(@PathVariable("collectionId") Long collectionId){
		return collectionService.findCollection(collectionId);
	}
	
	@PostMapping
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public Collection presistCollection(@RequestBody Collection collectionRequest) {
		
		Preconditions.checkArgument(StrUtil.isNotBlank(collectionRequest.getName()), "名称不能为空...");
		
		return collectionService.save(collectionRequest);
	}
	
	@PostMapping("/create")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public Collection createCollection(@RequestBody CollectionEditDTO collectionEditDTO) {
		
		Preconditions.checkArgument(StrUtil.isNotBlank(collectionEditDTO.getName()), "名称不能为空...");
		Preconditions.checkArgument(collectionEditDTO.getPresentId() != null, "展现方式不能为空...");
		
		return collectionService.createCollection(collectionEditDTO);
	}
	
	@PutMapping("/coverImg")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public Collection coverImgCollection(@RequestBody CollectionEditDTO collectionEditDTO) {
		
		Preconditions.checkArgument(StrUtil.isNotBlank(collectionEditDTO.getCoverImg()), "蒙层不能为空...");
		Preconditions.checkArgument(collectionEditDTO.getCollectionId() != null, "模块ID不能为空...");
		
		return collectionService.coverImgCollection(collectionEditDTO);
	}
	
	@PutMapping("/coverImgs")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public Boolean coverImgCollections(@RequestBody CollectionCoverDTO collectionCoverDTO) {
		
		Preconditions.checkArgument(StrUtil.isNotBlank(collectionCoverDTO.getCoverImg()), "蒙层不能为空...");
		Preconditions.checkArgument(collectionCoverDTO.getCollectionIds() != null && !collectionCoverDTO.getCollectionIds().isEmpty(), "模块ID不能为空...");
		
		collectionService.coverImgCollections(collectionCoverDTO);
		
		return Boolean.TRUE;
	}
	
	@PostMapping("/copy")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public Collection copyCollection(@RequestBody CollectionRequest collectionRequest) {
		
		Preconditions.checkArgument(StrUtil.isNotBlank(collectionRequest.getName()), "名称不能为空...");
		Preconditions.checkArgument(collectionRequest.getCollectionId() != null, "复制对象不能为空...");
		
		return collectionService.copyCollection(collectionRequest);
	}
	
	@PostMapping("/copys")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public List<CollectionRequest> copyCollections(@RequestBody List<CollectionRequest> collectionRequests) {
		
		List<CollectionRequest> errors = new ArrayList<>();
		
		for (CollectionRequest collectionRequest : collectionRequests) {
			try {
				Preconditions.checkArgument(StrUtil.isNotBlank(collectionRequest.getName()), "名称不能为空...");
				Preconditions.checkArgument(collectionRequest.getCollectionId() != null, "复制对象不能为空...");
				
				collectionService.copyCollection(collectionRequest);
			}catch (Exception e) {
				collectionRequest.setErrorMsg(e.getMessage());
				errors.add(collectionRequest);
			}
		}
		
		return errors ; 
	}
	
	@PostMapping("/comments")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public List<CollectionRequest> commentCollections(@RequestBody List<CollectionRequest> collectionRequests) {
		
		List<CollectionRequest> errors = new ArrayList<>();
		
		for (CollectionRequest collectionRequest : collectionRequests) {
			try {
				Preconditions.checkArgument(collectionRequest.getCollectionId() != null, "修改对象不能为空...");
				
				collectionService.commentCollection(collectionRequest);
			}catch (Exception e) {
				collectionRequest.setErrorMsg(e.getMessage());
				errors.add(collectionRequest);
			}
		}
		
		return errors ; 
	}
	
	
	@DeleteMapping("/{collectionId}")
	@PreAuthorize("hasAnyRoles('admin', 'shop')")
	public Boolean deleteCollection(@PathVariable("collectionId") Long collectionId) {
	
		return collectionService.deleteCollection(collectionId);
	}
	
	@DeleteMapping("/batch")
	@PreAuthorize("hasAnyRoles('admin', 'shop')")
	public Boolean deleteCollections(@RequestBody List<Long> collectionIds) {
	
		return collectionService.deleteCollections(collectionIds);
	}
	
	
	@PostMapping("/{collectionId}/products")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public Boolean bindCollectionProducts(@PathVariable("collectionId") Long collectionId , @RequestBody List<Long> products) {
		
		return collectionService.bindCollectionProducts(collectionId , products);
	}
	
	@DeleteMapping("/collection/products")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public Boolean deleteCollectionProducts(@RequestBody List<Long> collectionProductIds) {
		
		return collectionService.deleteCollectionProducts(collectionProductIds);
	}
	
	
	@GetMapping("/present")
	@PreAuthorize("hasAnyRoles('admin')")
	public List<Present> findPresent(){
		return presentService.findAllPresent();
	}
	
	@GetMapping("/present/{presentId}")
	@PreAuthorize("hasAnyRoles('admin')")
	public Present findPresent(@PathVariable("presentId") Long presentId){
		return presentService.findPresent(presentId);
	}
	
	@PostMapping("/present")
	@PreAuthorize("hasAnyRoles('admin')")
	public Present presistPresent(@RequestBody Present present){
		return presentService.save(present);
	}
	
	@DeleteMapping("/present/{presentId}")
	@PreAuthorize("hasAnyRoles('admin')")
	public Boolean deletePresent(@PathVariable("presentId") Long presentId){
		presentService.deletePresent(presentId);
		return Boolean.TRUE;
	}
	@GetMapping("/present/combobox")
	@PreAuthorize("hasLogin()")
	public List<Map<String , Object>> findPresentCombobox(){
		return presentService.findPresentCombobox();
	}
}
